package com.ruoyi.project.emr.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.emr.domain.EmrDrug;
import com.ruoyi.project.emr.mapper.EmrDrugMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.emr.mapper.EmrPrescriptionMapper;
import com.ruoyi.project.emr.domain.EmrPrescription;
import com.ruoyi.project.emr.service.IEmrPrescriptionService;

/**
 * 处方Service业务层处理
 * 
 * @author gzu
 * @date 2023-07-22
 */
@Service
public class EmrPrescriptionServiceImpl implements IEmrPrescriptionService 
{
    @Autowired
    private EmrPrescriptionMapper emrPrescriptionMapper;

    @Autowired
    private EmrDrugMapper drugMapper;

    /**
     * 查询处方
     * 
     * @param id 处方主键
     * @return 处方
     */
    @Override
    public EmrPrescription selectEmrPrescriptionById(Long id)
    {
        return emrPrescriptionMapper.selectEmrPrescriptionById(id);
    }

    /**
     * 查询处方列表
     * 
     * @param emrPrescription 处方
     * @return 处方
     */
    @Override
    public List<EmrPrescription> selectEmrPrescriptionList(EmrPrescription emrPrescription)
    {
        return emrPrescriptionMapper.selectEmrPrescriptionList(emrPrescription);
    }

    /**
     * 新增处方
     * 
     * @param emrPrescription 处方
     * @return 结果
     */
    @Override
    public int insertEmrPrescription(EmrPrescription emrPrescription)
    {
        emrPrescription.setCreateTime(DateUtils.getNowDate());
        return emrPrescriptionMapper.insertEmrPrescription(emrPrescription);
    }

    /**
     * 查找某病历开具时间最近的处方
     * @param medicalRecordId 病历号
     * @return 结果
     */
    @Override
    public EmrPrescription getLatestPrescription(String medicalRecordId) {
        return emrPrescriptionMapper.selectLatestPrescription(medicalRecordId);
    }


    @Override
    public int insertDrugsToPrescription(Integer preId, Integer drugId, Integer count) {
        if(preId < 0 || drugId < 0 || count < 1) {
            return 0;
        }
        return emrPrescriptionMapper.insertDrugsToPrescription(preId, drugId, count);
    }

    /**
     * 修改处方
     * 
     * @param emrPrescription 处方
     * @return 结果
     */
    @Override
    public int updateEmrPrescription(EmrPrescription emrPrescription)
    {
        emrPrescription.setUpdateTime(DateUtils.getNowDate());
        return emrPrescriptionMapper.updateEmrPrescription(emrPrescription);
    }

    /**
     * 批量删除处方
     * 
     * @param ids 需要删除的处方主键
     * @return 结果
     */
    @Override
    public int deleteEmrPrescriptionByIds(Long[] ids)
    {
        return emrPrescriptionMapper.deleteEmrPrescriptionByIds(ids);
    }

    /**
     * 删除处方信息
     * 
     * @param id 处方主键
     * @return 结果
     */
    @Override
    public int deleteEmrPrescriptionById(Long id)
    {
        return emrPrescriptionMapper.deleteEmrPrescriptionById(id);
    }


    @Override
    public List<EmrDrug> selectDrugsOfPrescription(Integer preId) {
        if(preId <= 0) {
            //todo exception
        }

        return drugMapper.selectDrugsOfPrescription(preId);
    }
}
